package cn.com.zhui.demo.workflow;

import org.activiti.bpmn.model.BpmnModel;
import org.activiti.bpmn.model.FlowElement;
import org.activiti.bpmn.model.SequenceFlow;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.impl.util.ProcessDefinitionUtil;
import org.activiti.engine.repository.ProcessDefinition;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@SpringBootTest
public class Part2_ProcessDefinition {

    @Autowired
    private RepositoryService repositoryService;

    //查询流程定义
    @Test
    public void getDefinitions(){
        List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery()
                .list();
        for(ProcessDefinition pd : list){
            System.out.println("------流程定义--------");
            System.out.println("Name："+pd.getName());
            System.out.println("Key："+pd.getKey());
            System.out.println("ResourceName："+pd.getResourceName());
            System.out.println("DeploymentId："+pd.getDeploymentId());
            System.out.println("Version："+pd.getVersion());
            BpmnModel bpmnModel = repositoryService.getBpmnModel(pd.getId());
            Collection<FlowElement> flowElements = bpmnModel.getProcesses().get(0).getFlowElements();
            List<String> flowName = flowElements.stream()
                    .filter(f -> f instanceof SequenceFlow)
                    .map(f -> (SequenceFlow) f)
                    .filter(f -> f.getName() != null)
                    .map(f -> f.getName())
                    .distinct()
                    .collect(Collectors.toList());
            System.out.println();;
        }
    }

    //删除流程定义
    @Test
    public void delDefinition(){

        String pdID="eca23cf7-4db4-11ec-9eab-0050568c3ce1";
        repositoryService.deleteDeployment(pdID,true);
        System.out.println("删除流程定义成功");
    }
}